def generate_name_map():
    name_map = {}
    with open(file=r"E:\IdeaProjects\jzjh3\wlhd\units\campaignunitfunc.txt", mode="r", encoding="utf-8") as f:
        current_key = ""
        for line in f.readlines():
            if line.startswith("["):
                current_key = line[1:5]
            if line.startswith("Name"):
                words = line.split("=")
                current_name = words[1]
                name_map[current_key] = current_name.rstrip("\n")
    return name_map


def handle_ability_name(name_map):
    path = "E:/IdeaProjects/jzjh3/jzjh3/table/"
    with open(file=path + "unit.ini", mode="r", encoding="utf-8") as f, open(file=path + "out.ini", mode="w",
                                                                             encoding="utf-8") as g:
        current_key = ""
        for line in f.readlines():
            print(line, end="")
            if line.startswith("["):
                current_key = line[1:5]
            if line.startswith("Name"):
                words = line.split("\"")
                current_name = nameMap.get(current_key, words[1])
                g.write('Name = "{0}"\n'.format(current_name))
                continue
            g.write(line)


if __name__ == '__main__':
    nameMap = generate_name_map()
    # print(nameMap)
    handle_ability_name(nameMap)
